/**
 * @author zhangmin
 * @create 2022-04-06 14:53
 */

class myThread implements Runnable{
    private int number=1;

    /*
    * 线程1：进入，释放锁
    * 线程2：进入，唤醒1，但是线程2锁住了，线程2释放锁
    * 线程1 进来   唤醒线程2  但是线程1锁住  线程1释放锁 线程2 进来
    * */
    @Override
    public void run() {
        synchronized (this){
            while (number<=100){
                notify();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":"+number);
                number++;
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            notifyAll();//重要，没有的话会无法结束
        }
    }
}
public class Mythread100 {
    public static void main(String[] args) {
        myThread myThread=new myThread();
        Thread t1=new Thread(myThread);
        Thread t2=new Thread(myThread);
        t1.start();
        t2.start();
    }
}
